<?php

declare (strict_types=1);

namespace App\Model;

use App\Library\Common;
use Hyperf\Database\Model\Events\Created;
use Hyperf\Database\Model\Events\Updated;
use Hyperf\Database\Model\Events\Saving;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasOne;

/**
 * @property int $id
 * @property int $shop_id 商户id
 * @property string $product_name 商品名称
 * @property int $channel_id 分类id=行业id
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property string $deleted_at 删除时间
 * @property int $create_by 创建人
 * @property int $update_by 修改人
 * @property int $delete_by 删除人
 * @property int $is_show 0隐藏  1显示
 * @property int $sales_initial 初始销量
 * @property int $sales_actual 实际销量
 * @property int $is_use_coupon 可用优惠券1可用 0不可用
 * @property int $is_index 是否首页1是0否
 * @property int $is_hot 是否推荐1是0否
 * @property int $is_sell 是否上架1是0否
 * @property int $min_quantity 起订量
 * @property int $is_origin_made 是否原产地 1是 0否
 * @property int $delivery_id 运费模板id
 * @property int $center_id 原产地id
 * @property int $check_status 1已审核  0未审核  -1驳回
 */
class Product extends CloudBase
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_product';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'shop_id' => 'integer', 'channel_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'create_by' => 'string', 'update_by' => 'string', 'delete_by' => 'string', 'is_show' => 'integer', 'sales_initial' => 'integer', 'sales_actual' => 'integer', 'is_use_coupon' => 'integer', 'is_index' => 'integer', 'is_hot' => 'integer', 'is_sell' => 'integer', 'min_quantity' => 'integer', 'is_origin_made' => 'integer', 'delivery_id' => 'integer', 'center_id' => 'integer', 'check_status' => 'integer'];

    public function link_channel()
    {
        $fields = ['fly_link_channel.*', 'fc.channel_name', 'fc.fileinfo_id', 'f.fileinfo_id', 'f.url', 'f.imagetype', 'f.mimetype'];
        return $this->hasMany(LinkChannel::class, 'link_id', 'id')
            ->leftJoin('fly_channel as fc', 'fc.id', '=', 'fly_link_channel.channel_id')
            ->leftJoin('fileinfo as f', 'f.fileinfo_id', '=', 'fc.fileinfo_id')
            ->where('fly_link_channel.type', 1)
            ->select($fields);
    }


//    public function center_info()
//    {
//        $fields = ['fdc.id', 'fdc.name','fly_shop.id as shop_id'];
//        return $this->belongsTo(FlyShop::class, 'shop_id', 'id')
//            ->leftJoin('fly_distribution_center as fdc','fdc.id','=','fly_shop.join_dc_id')
//            ->select($fields);
////        return $this->belongsTo(DistributionCenter::class, 'center_id', 'id')
////            ->select($fields);
//    }

    public function transport_info()
    {
        return $this->hasMany(TransportMode::class, 'product_id', 'id')->select();
    }

    public function specialty_info()
    {
        $fields = ['id', 'specialty_name'];
        return $this->belongsTo(Specialty::class, 'specialty_id', 'id')->select($fields);
    }

    public function main_pic()
    {
        $fields = ['tags', 'distribution_center_id'];
        return $this->hasMany(LinkFile::class, 'link_id', 'id')
            ->where('fly_link_file.type', 2)
            ->with('file_info')
            ->select();
    }

    public function spec_key()
    {
        return $this->hasMany(ProductSpecKey::class, 'product_id', 'id')
            ->with('values')
            ->select();
    }

    public function sku_list()
    {
        return $this->hasMany(Sku::class, 'product_id', 'id')
            ->with('spec_value')
            ->with('step_price')
            ->with('file_info')
            ->select();
    }

    public function detail_pic()
    {
        return $this->hasMany(LinkFile::class, 'link_id', 'id')
            ->where('fly_link_file.type', 3)
            ->with('file_info')
            ->select();
    }

//   SPU 商品最低价
    public function shop_min_sku()
    {
        return $this->belongsTo(Sku::class, 'id', 'product_id')
            ->orderBy('sku_min_price', 'desc')->select();
    }


    /**
     * 审核原因
     * @return object
     */
    public function reason_info()
    {
        $fields = ['fly_product_audit.id', 'fly_product_audit.created_at', 'fly_product_audit.updated_at', 'fly_product_audit.audit_at', 'fly_product_audit.product_id', 'refuse.reason_json', 'refuse.tips'];
        return $this->belongsTo(ProductAudit::class, 'id', 'product_id')
            ->leftJoin('fly_product_audit_refuse as refuse', 'refuse.audit_id', '=', 'fly_product_audit.id')
            ->where('fly_product_audit.audit_type', '1')
            ->orderBy('fly_product_audit.id', 'DESC')
            ->limit(1)
            ->select($fields);
    }

    /**
     * 驳回原因 下架原因
     * @return object
     */
    public function refuse_info()
    {
        $fields = ['fly_product_audit.id', 'fly_product_audit.created_at', 'fly_product_audit.updated_at', 'fly_product_audit.audit_at', 'fly_product_audit.product_id', 'fly_product_audit.created_at', 'fly_product_audit.audit_at', 'refuse.reason_json', 'refuse.tips'];
        return $this->belongsTo(ProductAudit::class, 'id', 'product_id')
            ->leftJoin('fly_product_audit_refuse as refuse', 'refuse.audit_id', '=', 'fly_product_audit.id')
            ->where('fly_product_audit.audit_type', '2')
            ->orderBy('fly_product_audit.id', 'DESC')
            ->limit(1)
            ->select($fields);
    }

    /**
     * 关联店铺
     * @return HasOne
     */
    public function with_shop()
    {
        $fields = ['shop_name', 'id', 'business_scope', 'join_dc_id', 'type', 'business_scope', 'shop_desc', 'link_name', 'phone', 'lat', 'lng'];
        return $this->hasOne(FlyShop::class, 'id', 'shop_id')
            ->with(['with_join_dc'])
            ->select($fields);
    }

    /**
     * 关联审核信息
     * @return object
     */
    public function audit_info()
    {
        $fields = ['fly_product_audit.id', 'fly_product_audit.created_at', 'fly_product_audit.updated_at', 'fly_product_audit.audit_at', 'fly_product_audit.product_id', 'fly_product_audit.created_at', 'fly_product_audit.audit_at', 'refuse.reason_json', 'refuse.tips'];
        return $this->belongsTo(ProductAudit::class, 'id', 'product_id')
            ->leftJoin('fly_product_audit_refuse as refuse', 'refuse.audit_id', '=', 'fly_product_audit.id')
            ->where('fly_product_audit.audit_type', '1')
            ->orderBy('fly_product_audit.id', 'DESC')
            ->limit(1)
            ->select($fields);
    }

    /**
     * 关联认证信息
     * @return object
     */
    public function certify_info()
    {
        $fields = ['id', 'product_id', 'audit_type', 'created_at', 'audit_at', 'audit_status'];
        return $this->belongsTo(ProductAudit::class, 'id', 'product_id')
            ->where('audit_type', '1')
            ->select($fields);
    }

    /**
     * 关联推广商品
     */
//    public function with_sharing_product()
//    {
//        return $this->belongsTo(SharingProduct::class, 'id', 'product_id');
//    }

    /**
     * 关联美食分类详情
     * @return BelongsTo
     */
    public function cate_info()
    {
        return $this->belongsTo(FlyGourmetMealClassify::class, 'category_id', 'id');
    }

    public function reviews_list(){
        return $this->hasMany(ShopOrderReview::class, 'product_id', 'id')
            ->leftJoin('app_user', 'fly_shop_order_reviews.reviewer', '=', 'app_user.id')
            ->select(['fly_shop_order_reviews.*', 'app_user.nickname', 'app_user.avatar'])
            ->orderBy('fly_shop_order_reviews.id','DESC')
            ->limit(5);
    }
    public function reviews(){
        return $this->hasMany(ShopOrderReview::class, 'product_id', 'id');
    }

}